import { getUserInfo, showToast, wsUrl } from '@/utils/index'
import { usePrivateChatStore } from '@/stores/privateChat'
import { useSystemNotificationStore } from '@/stores/system_notification'

// WebSocket工具类
class WebSocketManager {
  private socket: any | null = null;
  private static instance: WebSocketManager;
  private messageCallback: Function | null = null;
  private userId: number | null = null;
  private connectSuccessCallback: Function | null = null;
  private connectErrorCallback: Function | null = null;
  private reconnectTimer: any = null;
  private reconnectAttempts: number = 0;
  private maxReconnectAttempts: number = 5;
  private isManualClose: boolean = false; // 添加手动关闭标志

  // 单例模式
  public static getInstance(): WebSocketManager {
    if (!WebSocketManager.instance) {
      WebSocketManager.instance = new WebSocketManager();
    }
    return WebSocketManager.instance;
  }

  constructor() {
  }

  // 初始化WebSocket连接
  public init(
    onMessage: Function, 
    onSuccess?: Function, 
    onError?: Function
  ): boolean {
    // 从本地存储获取用户信息
    const userInfo: any = getUserInfo();
    if (!userInfo || !userInfo.user_id) {
      console.log('[WebSocket] 未登录用户，无法建立连接');
      this.close(); // 关闭连接
      return false; // 返回连接失败
    }
    
    const userId = userInfo.user_id;
    
    // 如果已经连接并且是同一个用户，不需要重新连接
    if (this.socket && this.userId === userId) {
      // 检查连接状态，微信小程序中通过 readyState 属性判断
      if (this.isConnected()) {
        this.messageCallback = onMessage;
        if (onSuccess) onSuccess();
        return true;
      }
    }
    
    this.userId = userId;
    this.messageCallback = onMessage;
    this.connectSuccessCallback = onSuccess || null;
    this.connectErrorCallback = onError || null;
    this.isManualClose = false; // 重置手动关闭标志
    
    // 清除之前的重连定时器
    if (this.reconnectTimer) {
      clearTimeout(this.reconnectTimer);
      this.reconnectTimer = null;
    }
    
    // 创建WebSocket连接 - 兼容微信小程序
    // 微信小程序中使用 uni.connectSocket
    this.socket = uni.connectSocket({
      url: `${wsUrl}?userId=${userId}`,
      complete: (res) => {
        console.log('[WebSocket] 连接响应:', res)
      }
    });
    
    this.socket.onOpen(() => {
      console.log('[WebSocket] 连接已建立');
      if (this.connectSuccessCallback) {
        this.connectSuccessCallback();
      }
    });
    
    this.socket.onMessage((event: any) => {
      try {
        const data = JSON.parse(event.data);
        console.log('[WebSocket] 响应:', data)
        this.handleMessage(data);
      } catch (e) {
        console.error('解析WebSocket消息失败:', e);
      }
    });
    
    this.socket.onClose(() => {
      console.log('[WebSocket] 连接已关闭');
      // 只有非手动关闭才尝试重连
      if (!this.isManualClose) {
        this.handleReconnect(onMessage, onSuccess, onError);
      }
    });
    
    this.socket.onError((error: any) => {
      console.error('[WebSocket] 连接错误:', error);
      if (this.connectErrorCallback) {
        this.connectErrorCallback(error);
      }
      // 只有非手动关闭才尝试重连
      if (!this.isManualClose) {
        this.handleReconnect(onMessage, onSuccess, onError);
      }
    });
    
    return true;
  }


  // 处理重连
  public handleReconnect(
    onMessage: Function,
    onSuccess?: Function,
    onError?: Function
  ): void {
    // 先检查是否还能重连
    if (this.reconnectAttempts >= this.maxReconnectAttempts) {
      console.log('[WebSocket] 重连次数已达上限');
      showToast('连接断开，请检查网络');
      this.reconnectAttempts = 0; // 重置重连次数
      return;
    }
    
    // 检查用户是否仍然登录
    const userInfo: any = getUserInfo();
    if (!userInfo || !userInfo.user_id) {
      console.log('[WebSocket] 用户未登录，停止重连');
      this.close(); // 关闭连接
      return;
    }
    
    // 记录重连尝试次数
    console.log(`[WebSocket] 尝试重连(${this.reconnectAttempts + 1}/${this.maxReconnectAttempts})`);
    
    this.reconnectTimer = setTimeout(() => {
      this.reconnectAttempts++; // 在真正重连时再增加重连次数
      this.init(onMessage, onSuccess, onError);
    }, 2000 * (this.reconnectAttempts + 1)); // 逐步增加重连间隔
  }

  // ---------------------------（收到消息）----------------------------
  private handleMessage(data: any): void {
    // 获取 Pinia store 实例
    const privateChatStore = usePrivateChatStore();
    const systemNotificationStore = useSystemNotificationStore();

    switch (data.type) {
      case 'connection_success':
        // 连接成功消息
        console.log('WebSocket连接成功:', data.msg);
        if (this.connectSuccessCallback) {
          this.connectSuccessCallback();
        }
        break;
      case 'connection_error':
        // 错误消息处理
        showToast(data.msg || '服务器错误');
        break;
      case 'private_message_send_message':
        // 发送私聊消息成功 添加到 Pinia store
        privateChatStore.handlePrivateMessageSendSuccess(data.data);
        break;
      case 'private_message':
        // 接收到私聊消息 添加到 Pinia store
        privateChatStore.handlePrivateMessageReceived(data.data);
        break;
      case 'private_message_read_success':
        // 接收到消息已读成功
        privateChatStore.handlePrivateMessageReadSuccess(data.message_id);
        break;

      case 'system_notification':
        // 系统通知消息 添加到系统通知 store
        systemNotificationStore.handleSystemNotificationReceived(data.data);
        break;
      case 'system_notification_read_success':
        // 接收到系统通知已读成功
        systemNotificationStore.handleSystemNotificationReadSuccess(data.notification_id);
        break;

      case 'error':
        // 错误消息处理
        showToast(data.msg || '服务器错误');
        console.error(data.msg || '服务器错误');
        break;

      default:
        // 错误消息处理
        showToast(data.msg || '未知的WebSocket消息类型:' + data.type);
        console.warn('', data.type);
    }

    // 调用回调函数
    if (this.messageCallback) {
      this.messageCallback(data);
    }
  }

  // ---------------------------（发送消息）----------------------------
  public sendMessage(message: any): void {
    if (this.isConnected()) {
      this.socket.send({
        data: JSON.stringify(message)
      });
    } else {
      showToast('连接未建立，正在尝试重连，请重试');

      // 尝试重新连接
      if (this.messageCallback) {
        this.init(this.messageCallback, this.connectSuccessCallback, this.connectErrorCallback);
      }
    }
  }

  // 注册消息回调（用于不同组件注册自己的处理逻辑）
  public registerCallback(onMessage: Function): void {
    this.messageCallback = onMessage;
  }

  // 关闭连接
  public close(isManual: boolean = true): void {
    this.isManualClose = isManual; // 设置手动关闭标志
    if (this.socket) {
      this.socket.close();
      this.socket = null;
    }
    // 清除重连定时器
    if (this.reconnectTimer) {
      clearTimeout(this.reconnectTimer);
      this.reconnectTimer = null;
    }
    this.reconnectAttempts = 0; // 重置重连次数
  }

  // 检查连接状态
  public isConnected(): boolean {
    // 微信小程序中的连接状态判断方式
    return this.socket !== null && typeof this.socket.readyState !== 'undefined' && this.socket.readyState === 1;
  }
  
  // 销毁实例，清理监听器
  public destroy(): void {
    // 关闭WebSocket连接
    this.close();
  }
}

// 导出单例实例
export default WebSocketManager.getInstance();